package com.example.repair.adapter;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.repair.R;
import com.example.repair.activity.order.ChangeActivity;
import com.example.repair.base.netWork.Constant;
import com.example.repair.entity.OrderBean;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

public class AllOrderAdapter extends RecyclerView.Adapter{

    private Context context;

    private String type;


    List<OrderBean.DataBean> allorder;

    public AllOrderAdapter(Context context,String type) {
        this.context = context;

        this.type= type;

        allorder = new ArrayList<>();

    }

    public void setAllorder(List<OrderBean.DataBean> allorder) {
        this.allorder = allorder;
        notifyDataSetChanged();
    }
    public void addAllorder(List<OrderBean.DataBean> allorder) {
        this.allorder.addAll(allorder);
        notifyDataSetChanged();
    }
    public void clear() {
        this.allorder.clear();
        notifyDataSetChanged();
    }



    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        ViewHolder viewHolder;
        ViewHolderFinish viewHolderFinish;
        ViewHolderFail viewHolderFail;
        View view;

        switch (allorder.get(i).getOrderStatus()){

            case 2://已经分配
                view= LayoutInflater.from(context).inflate(R.layout.recy_fenpei, viewGroup, false);
                viewHolder=new ViewHolder(view);
                return viewHolder;
            case 3://正在维修
                view= LayoutInflater.from(context).inflate(R.layout.recy_repair, viewGroup, false);
                viewHolder=new ViewHolder(view);
                return viewHolder;

            case 4://已支付
                view= LayoutInflater.from(context).inflate(R.layout.recy_pay, viewGroup, false);
                viewHolderFinish=new ViewHolderFinish(view);
                return viewHolderFinish;

            case 5://已完成
                view= LayoutInflater.from(context).inflate(R.layout.recy_finish, viewGroup, false);
                viewHolderFinish=new ViewHolderFinish(view);
                return viewHolderFinish;
            default:
                view= LayoutInflater.from(context).inflate(R.layout.recy_fail, viewGroup, false);
                viewHolderFail=new ViewHolderFail(view);
                return viewHolderFail;
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {


        ViewHolder viewHolderall;
        ViewHolderFinish viewHolderFinish;
        ViewHolderFail viewHolderFail;
        switch (allorder.get(i).getOrderStatus()){
            case 2://已经分配


                viewHolderall= (ViewHolder) viewHolder;
                if (!type.equals("1")){
                    viewHolderall.top.setVisibility(View.GONE);
                }
                viewHolderall.type.setText(allorder.get(i).getProduct_name());
                viewHolderall.name.setText(allorder.get(i).getUserName());
                viewHolderall.phone.setText(allorder.get(i).getUserPhone());
                viewHolderall.address.setText(allorder.get(i).getUserAddress());
                viewHolderall.status.setText(allorder.get(i).getOrderStatusName());

                final double lat = Double.parseDouble(allorder.get(i).getLat());
                final double log = Double.parseDouble(allorder.get(i).getLog());

                viewHolderall.start.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClick.item(i,allorder.get(i).getOrderId());
                    }
                });

                ((ViewHolder) viewHolder).daohang.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClickD.item(v,lat,log);
                    }
                });

                viewHolderall.phone.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        check(allorder.get(i).getUserPhone());
                    }
                });



                break;
            case 3://正在维修
                viewHolderall= (ViewHolder) viewHolder;
                if (!type.equals("1")){
                    viewHolderall.top.setVisibility(View.GONE);
                }
                viewHolderall.type.setText(allorder.get(i).getProduct_name());
                viewHolderall.name.setText(allorder.get(i).getUserName());
                viewHolderall.phone.setText(allorder.get(i).getUserPhone());
                viewHolderall.address.setText(allorder.get(i).getUserAddress());
                viewHolderall.status.setText(allorder.get(i).getOrderStatusName());
                viewHolderall.start.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context, ChangeActivity.class);
                        intent.putExtra("orderId",allorder.get(i).getOrderId()+"");
                        context.startActivity(intent);
                    }
                });

                break;
            case 4://已支付
            case 5://已完成
                viewHolderFinish= (ViewHolderFinish) viewHolder;
                if (!type.equals("1")){
                    viewHolderFinish.top.setVisibility(View.GONE);
                }
                viewHolderFinish.type.setText(allorder.get(i).getProduct_name());
                viewHolderFinish.name.setText(allorder.get(i).getUserName());
                viewHolderFinish.phone.setText(allorder.get(i).getUserPhone());
                viewHolderFinish.address.setText(allorder.get(i).getUserAddress());
                viewHolderFinish.status.setText(allorder.get(i).getOrderStatusName());
                viewHolderFinish.text.setText(allorder.get(i).getOrderRemarks()==null||allorder.get(i).getOrderRemarks().equals("")?"无":allorder.get(i).getOrderRemarks());
                viewHolderFinish.money.setText(allorder.get(i).getTotalMoney());
                viewHolderFinish.pay_image.setImageURI(Constant.PATH+allorder.get(i).getVoucher_img());
                break;
            default:
                viewHolderFail= (ViewHolderFail) viewHolder;
                if (!type.equals("1")){
                    viewHolderFail.top.setVisibility(View.GONE);
                }
                viewHolderFail.type.setText(allorder.get(i).getProduct_name());
                viewHolderFail.name.setText(allorder.get(i).getUserName());
                viewHolderFail.phone.setText(allorder.get(i).getUserPhone());
                viewHolderFail.address.setText(allorder.get(i).getUserAddress());
                viewHolderFail.status.setText(allorder.get(i).getOrderStatusName());
                viewHolderFail.text.setText(allorder.get(i).getOrderRemarks()==null||allorder.get(i).getOrderRemarks().equals("")?"无":allorder.get(i).getOrderRemarks());
                break;
        }

    }

    private void check(String phone) {
        // 检查是否获得了权限（Android6.0运行时权限）
        //权限没有获得
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            // 没有获得授权，申请授权
            if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
                    Manifest.permission.CALL_PHONE)) {
                // 返回值：
                // 如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
                // 如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
                // 如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
                // 弹窗需要解释为何需要该权限，再次请求授权
                Toast.makeText(context, "请授权拨打电话权限！", Toast.LENGTH_LONG).show();

                // 帮跳转到该应用的设置界面，让用户手动授权
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", context.getPackageName(), null);
                intent.setData(uri);
                context.startActivity(intent);
            } else {
                // 不需要解释为何需要该权限，直接请求授权
                ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE}, 100);
            }
        } else {
            // 已经获得授权，可以打电话
            CallPhone(phone);
        }
    }

    private void CallPhone(String phone) {
        // 拨号：激活系统的拨号组件
        Intent intent = new Intent(); // 意图对象：动作 + 数据
        intent.setAction(Intent.ACTION_DIAL); // 设置动作
        Uri data = Uri.parse("tel:" + phone); // 设置数据
        intent.setData(data);
        context.startActivity(intent); // 激活Activity组件

    }

    @Override
    public int getItemCount() {
        return allorder.size();
    }




    //判断等于几时加载那种布局
    @Override
    public int getItemViewType(int position) {
        return position;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public LinearLayout top;
        public TextView type;
        public TextView name;
        public TextView phone;
        public TextView address;
        public TextView status;
        public Button start;
        public Button daohang;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            top=itemView.findViewById(R.id.top);
            type=itemView.findViewById(R.id.type);
            name=itemView.findViewById(R.id.name);
            phone=itemView.findViewById(R.id.phone);
            address=itemView.findViewById(R.id.address);
            status=itemView.findViewById(R.id.status);
            start=itemView.findViewById(R.id.start);
            daohang=itemView.findViewById(R.id.daohang);
        }
    }
    class ViewHolderFinish extends RecyclerView.ViewHolder {

        public LinearLayout top;
        public TextView type;
        public TextView name;
        public TextView phone;
        public TextView address;
        public TextView status;
        public TextView money;
        public TextView text;
        public SimpleDraweeView pay_image;

        public ViewHolderFinish(@NonNull View itemView) {
            super(itemView);
            top=itemView.findViewById(R.id.top);
            type=itemView.findViewById(R.id.type);
            name=itemView.findViewById(R.id.name);
            phone=itemView.findViewById(R.id.phone);
            address=itemView.findViewById(R.id.address);
            status=itemView.findViewById(R.id.status);
            money=itemView.findViewById(R.id.money);
            text=itemView.findViewById(R.id.text);
            pay_image=itemView.findViewById(R.id.pay_image);
        }
    }
    class ViewHolderFail extends RecyclerView.ViewHolder {

        public LinearLayout top;
        public TextView type;
        public TextView name;
        public TextView phone;
        public TextView address;
        public TextView status;
        public TextView text;

        public ViewHolderFail(@NonNull View itemView) {
            super(itemView);
            top=itemView.findViewById(R.id.top);
            type=itemView.findViewById(R.id.type);
            name=itemView.findViewById(R.id.name);
            phone=itemView.findViewById(R.id.phone);
            address=itemView.findViewById(R.id.address);
            status=itemView.findViewById(R.id.status);
            text=itemView.findViewById(R.id.text);
        }
    }

    private OnClick onClick;
    public void setdeleteClick(OnClick onClick) {
        this.onClick = onClick;
    }
    public interface OnClick{
        void item(int posttion,int orderId);
    }


    private OnClickD onClickD;
    public void setdeleteClick(OnClickD onClick) {
        this.onClickD = onClick;
    }
    public interface OnClickD{
        void item(View v,double lat,double log);
    }



}
